package da.metricscraft.client.threads;

import java.awt.Point;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import da.metricscraft.client.ArrayList2D;

public class BiomsXMLLoader extends XMLLoader<ArrayList2D<String, String>> {
	private ArrayList2D<String, String> list;
	private ArrayList2D<String, String> biomsList;
	private Point lowest;
	private Point highest;
	public BiomsXMLLoader(LoaderTypes type) {
		super(type);
		list = new ArrayList2D<String, String>();
		biomsList = new ArrayList2D<String, String>();
		lowest = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
		highest = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
	}
	public Point getLowest(){ return lowest; }
	public Point getHighest(){ return highest; }

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			if (path != ""){
				readXML();

				NodeList nl = xmldoc.getElementsByTagName("topblock");

				for(int i = 0; i < nl.getLength(); i++){
					
					NamedNodeMap attr = nl.item(i).getAttributes();
					int x = Integer.parseInt(attr.getNamedItem("x").getTextContent());
					int z = Integer.parseInt(attr.getNamedItem("z").getTextContent());
					list.put(attr.getNamedItem("x").getTextContent(), 
							attr.getNamedItem("z").getTextContent(), 
							attr.getNamedItem("type").getTextContent());
					if (lowest.x > x){
						lowest.x = x;
					}else if (highest.x < x){
						highest.x = x;
					}
					if (lowest.y > z){
						lowest.y = z;
					}else if (highest.y < z){
						highest.y = z;
					}
					biomsList.put(x + "", z + "", nl.item(i).getParentNode().getNodeName());
				}
				data = list;
				System.out.println("File " + path.substring(path.lastIndexOf('\\') + 1) + " successfully read!");
				path = "";
				dispatch();
			}else{
				try {
					synchronized(this){
						this.wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public ArrayList2D<String, String> getBiomesMap(){
		return biomsList;
	}

}
